package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.QuestionRecordEntity;
import io.renren.modules.lanling.service.QuestionRecordService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 题目记录表（记录顺序练习的题目）
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-11-02 16:58:39
 */
@RestController
@RequestMapping("lanling/questionrecord")
public class QuestionRecordController {
    @Autowired
    private QuestionRecordService questionRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:questionrecord:list")
    public R list(@RequestParam Map <String, Object> params) {
        Long userId = getUserId();
        params.put("createUserId", userId);
        PageUtils page = questionRecordService.queryPage( params );
        return R.ok().put( "page", page );
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:questionrecord:info")
    public R info(@PathVariable("id") Integer id) {
        QuestionRecordEntity questionRecord = questionRecordService.selectById( id );

        return R.ok().put( "questionRecord", questionRecord );
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:questionrecord:save")
    public R save(@RequestBody QuestionRecordEntity questionRecord) {
        questionRecordService.insert( questionRecord );
        questionRecord.setCreateDate(new Date());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:questionrecord:update")
    public R update(@RequestBody QuestionRecordEntity questionRecord) {
        ValidatorUtils.validateEntity( questionRecord );
        questionRecordService.updateAllColumnById( questionRecord );//全部更新
        questionRecord.setLastModifyDate(new Date());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:questionrecord:delete")
    public R delete(@RequestBody Integer[] ids) {
        questionRecordService.deleteBatchIds( Arrays.asList( ids ) );

        return R.ok();
    }

}
